package com.goodmean.teacher.instructor;

import com.goodmean.common.User;
import com.goodmean.common.VO.TeacherGradeModel;
import com.goodmean.common.model.*;
import com.goodmean.student.StudentService;
import com.goodmean.teacher.admin.ClazzCourseService;
import com.goodmean.teacher.admin.RewardPunishInfomationService;
import com.goodmean.teacher.teacher.TeacherService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

import java.util.List;

@Before(InstructorInterceptor.class)
public class InstructorController extends Controller {
    private InstructorService service = InstructorService.me;
    private TeacherService teacherService = TeacherService.me;
    private StudentService studentService = StudentService.me;
    private RewardPunishInfomationService rewardPunishInfomationService = RewardPunishInfomationService.me;
    private ClazzCourseService clazzCourseService = ClazzCourseService.me;

    public void index() {
        render("instructor.html");
    }

    /**
     * 管理的所有班级信息
     **/
    public void allClazz() {
        User user = getSessionAttr("user");
        setAttr("clazz", service.findByID(user.getId()));
    }

    /**
     * 查看班级的学生信息
     **/
    @Before(InstructorValidator.class)
    public void oneClazz() {
        Integer clazz_id = getParaToInt("clazz_id");
        String student_name = getPara("student_name" , "");
        Integer pageNo = getParaToInt("pageNo", 1);
        setAttr("students", studentService.findByClazzId(clazz_id,student_name , pageNo));
        setAttr("clazz_id" , clazz_id);
    }
    /**
     * 管理班级的学生信息
     **/
    public void student() {
        String student_id = getPara("student_id");
        Student student = studentService.findByid(student_id);
        setAttr("student", student);
        setAttr("information", rewardPunishInfomationService.findByStuId(student.getStudentId()));
    }
    /**
     * 管理班级的学生奖惩信息
     **/
    public void addStuRe() {
        setAttr("student_id", getPara("student_id"));
        render("addStuRe.html");
    }

    public void saveStuRe() {
        setAttr("student_id", getPara("student_id"));
        RewardPunishInformation rewardPunishInformation = getModel(RewardPunishInformation.class, "");
        rewardPunishInfomationService.add(rewardPunishInformation);
        Student student = studentService.findByid(rewardPunishInformation.getStudentId());
        setAttr("student", student);
        setAttr("information", rewardPunishInfomationService.findByStuId(student.getStudentId()));
        render("student.html");
    }

    /**
     * 修改个人信息页面
     **/
    public void update() {
        User user = getSessionAttr("user");
        Teacher teacher = teacherService.findById(Integer.valueOf(user.getId()));
        setAttr("teacher", teacher);
        render("update.html");
    }

    /**
     * 修改个人信息
     **/
    public void save() {
        User user = getSessionAttr("user");
        Integer teacher_id = Integer.valueOf(user.getId());
        // 页面的modelName正好是Blog类名的首字母小写
        Teacher teacher1 = getModel(Teacher.class, "");
        teacherService.update(teacher1, teacher_id);
        Teacher teacher = teacherService.findById(teacher_id);
        setAttr("teacher", teacher);
        render("update.html");
    }

    /**
     * 查看个人课表
     **/
    public void course() {
        User user = getSessionAttr("user");
        setAttr("courses", teacherService.findMyCourse("teacher_id", Integer.valueOf(user.getId())));
        render("course.html");
    }

    /**
     * 查找某个班级某个课程成绩
     */
    public void findScore() {
        User loginUser = getSessionAttr("user");
        setAttr("clazzCourses", clazzCourseService.findByTeacherId(Integer.parseInt(loginUser.getId())));
        render("clazzGrade.html");
    }

    public void score() {
        Integer clazzId = getParaToInt("clazzId");
        Integer courseId = getParaToInt("courseId");
        System.out.println("clazzId:" + clazzId + " courseId:" + courseId);
        setCourse(clazzId, courseId);
    }
    /**
     * 修改某个班级全部成绩
     */
    public void updateScore() {
        Integer clazz_id = getParaToInt("clazzId");
        Integer course_id = getParaToInt("courseId");
        List<Student> students = studentService.findByClazzId(clazz_id);
        String msg = "修改学生成绩成功！";
        Integer grade;
        for (Student s : students) {
            grade = getParaToInt(s.getStudentId());
            if(!service.saveStudentGrade(grade, new CourseStudentGrade().setCourseId(course_id).setStudentId(s.getStudentId()).setCourseGrade(grade)))
                msg = "修改学生成绩失败！";
        }
        setAttr("msg", msg);
        setCourse(clazz_id, course_id);
    }

    private void setCourse(Integer clazzId, Integer courseId) {
        TeacherGradeModel teacherGradeModel = teacherService.findAllStuGrades(clazzId, courseId);
        setAttr("tgm", teacherGradeModel);
        render("findClazzGrades.html");
    }

}